home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Devices / SCSI Simple Sample / Src / SCSICheckForDevicePresent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  2.4 KB  |  88 lines  |  [TEXT/KAHL]

  1. /*                            SCSICheckForDevicePreseent.c                        */
  2. /*
  3.  * SCSICheckForDevicePreseent.c
  4.  * Copyright © 1992-94 Apple Computer Inc. All Rights Reserved.
  5.  *
  6.  * Return TRUE if the indicated device is present on this system. This function
  7.  * only logs unexpected errors..
  8.  */
  9. #include "SCSISimpleSample.h"
  10.  
  11. Boolean
  12. SCSICheckForDevicePresent(
  13.         DeviceIdent                scsiDevice,
  14.         Boolean                    enableAsynchSCSI
  15.     )
  16. {
  17.         Boolean                        result;
  18.         ScsiCmdBlock                scsiCmdBlock;
  19.         SCSI_Inquiry_Data            inquiry;
  20. #define SCB        (scsiCmdBlock)
  21. #define SENSE    (SCB.sense)
  22.         
  23.         if (gVerboseDisplay)
  24.             ShowSCSIBusID(scsiDevice, "\pChecking for device presence");
  25.         CLEAR(SCB);
  26.         SCB.scsiDevice = scsiDevice;
  27.         SCB.command.scsi6.opcode = kScsiCmdInquiry;
  28.         SCB.command.scsi6.len = sizeof inquiry;
  29.         SCB.bufferPtr = (Ptr) &inquiry;
  30.         SCB.transferSize = sizeof inquiry;
  31.         SCB.transferQuantum = 1;                        /* Force handshake        */
  32.         /* All other command bytes are zero */
  33.         DoSCSICommandWithSense(&scsiCmdBlock, FALSE, enableAsynchSCSI);
  34.         switch (SCB.status) {
  35.         case noErr:
  36.             /*
  37.              * The Quantum disk returns an "illegal" device type (actually == 127)
  38.              * if you call using a SCSI-1 command with an non-zero LUN.
  39.              */
  40.             if (inquiry.devType >= kScsiDevTypeMax) {
  41.                 VERBOSE("\pInquiry returned illegal device type.");
  42.                 result = FALSE;
  43.             }
  44.             else {
  45.                 VERBOSE("\pDevice is present");
  46.                 result = TRUE;
  47.             }
  48.             break;
  49.         case statusErr:
  50.             /*
  51.              * The target returned Check Condition. We need to look at the sense
  52.              * data, if any, to distinguish between an "offline" but present device,
  53.              * and a non-existant logical unit. Note: some drives return Check
  54.              * Condition, and "no sense error" if we try to access an incorrect
  55.              * logical unit. This might reasonably be remapped as "illegal request.
  56.              */
  57.             if (SCB.requestSenseStatus == noErr
  58.              && (SENSE.errorCode & kScsiSenseInfoMask) != kScsiSenseInfoValid)
  59.                  result = 0;
  60.              else {
  61.                 switch (SENSE.senseKey) {
  62.                 case kScsiSenseIllegalReq:
  63.                     result = FALSE;
  64.                     break;
  65.                 default:
  66.                     ShowRequestSense(&scsiCmdBlock);
  67.                     result = TRUE;
  68.                     break;
  69.                 }
  70.             }
  71.             break;    
  72.         default:                                /* Strange error        */
  73.             ShowRequestSense(&scsiCmdBlock);
  74.             /* Fall through */
  75.         case scsiDeviceNotThere:
  76.         case scsiSelectTimeout:
  77.         case scsiBusInvalid:
  78.         case scsiTIDInvalid:
  79.         case scCommErr:                            /* No such device        */
  80.         case scsiIdentifyMessageRejected:
  81.             result = FALSE;
  82.             break;
  83.         }
  84.         return (result);
  85. #undef SCB
  86. }
  87.  
  88.